package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkTeam;
import com.zb.entity.ZjWorkTeamEmp;
import com.zb.mapper.ZjWorkTeamEmpMapper;
import com.zb.service.ZjWorkTeamEmpService;
import com.zb.vo.WorkTeamEmpVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单客服组人员表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-12
 */
@Service
public class ZjWorkTeamEmpServiceImpl extends ServiceImpl<ZjWorkTeamEmpMapper, ZjWorkTeamEmp> implements ZjWorkTeamEmpService {

    // 新增、修改、删除客服组人员
    @Override
    public void save(Long custaccId, Long workTeamId, List<WorkTeamEmpVo> teamEmps, List<Long> delTeamEmpIds) {
        if (null != delTeamEmpIds || !delTeamEmpIds.isEmpty()) {
            this.removeByIds(delTeamEmpIds);
        }
        if (null == teamEmps || teamEmps.isEmpty()) {
            return;
        }
        List<ZjWorkTeamEmp> adds = new ArrayList<>();
        List<ZjWorkTeamEmp> updates = new ArrayList<>();
        ZjWorkTeamEmp emp = null;
        for (WorkTeamEmpVo empVo : teamEmps) {
            emp = new ZjWorkTeamEmp();
            BeanUtils.copyProperties(empVo, emp);
            if (null != emp.getId()) {
                updates.add(emp);
                continue;
            }
            emp.setId(IdWorker.getId());
            emp.setCustAccId(custaccId);
            emp.setWorkTeamId(workTeamId);
            adds.add(emp);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据工单客服组ID查询客服组人员
    @Override
    public List<ZjWorkTeamEmp> getByWorkTeamIds(List<Long> workTeamIds) {
        if (null == workTeamIds || workTeamIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkTeamEmp> wrapper = new QueryWrapper<>();
        wrapper.in("work_team_id", workTeamIds);
        return this.list(wrapper);
    }

    @Override
    public List<ZjWorkTeamEmp> getByWorkTeamId(Long workTeamId) {
        QueryWrapper<ZjWorkTeamEmp> wrapper = new QueryWrapper<>();
        wrapper.eq("work_team_id", workTeamId);
        return this.list(wrapper);
    }

    // 根据工单客服组ID集合删除客服组人员
    @Override
    public void deleteBatchByWorkTeamId(List<Long> workTeamIds) {
        if (null == workTeamIds || workTeamIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjWorkTeamEmp> wrapper = new QueryWrapper<>();
        wrapper.in("work_team_id", workTeamIds);
        this.remove(wrapper);
    }
}
